#!/bin/sh
set -e

BUILD="$(pwd)"

USER_OPTS="$@"
if test -f "$BUILD"/ffmpeg_options ; then
    USER_OPTS="$(cat "$BUILD"/ffmpeg_options) $USER_OPTS"
fi
OPTIONS="--enable-gpl --disable-debug --disable-doc"

if "$BUILD"/scripts/test-libmpv ; then
    OPTIONS="$OPTIONS --enable-pic"
fi

OPTIONS="$OPTIONS $USER_OPTS"

# Do FFmpeg's job.
if ! ( echo "$OPTIONS" | grep -q -e --enable-openssl ) &&
   ! ( echo "$OPTIONS" | grep -q -e --enable-gnutls ) ;
then
    if pkg-config gnutls ; then
        OPTIONS="$OPTIONS --enable-gnutls"
        echo "Auto-enabling GnuTLS."
    elif pkg-config openssl ; then
        OPTIONS="$OPTIONS --enable-nonfree --enable-openssl"
        echo "Auto-enabling OpenSSL (creates a non-redistributable binary)."
    fi
fi

case "$PKG_CONFIG_PATH" in
  '')
    export PKG_CONFIG_PATH="$BUILD/build_libs/lib/pkgconfig"
    ;;
  *)
    export PKG_CONFIG_PATH="$BUILD/build_libs/lib/pkgconfig:$PKG_CONFIG_PATH"
    ;;
esac

echo Using ffmpeg options: $OPTIONS

mkdir -p "$BUILD"/ffmpeg_build
cd "$BUILD"/ffmpeg_build
"$BUILD"/ffmpeg/configure --prefix="$BUILD"/build_libs --enable-static --disable-shared $OPTIONS
